package com.example.keli.ui.list;

import android.content.Context;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.keli.MainActivity;
import com.example.keli.R;
import com.example.keli.api.HttpUtils;
import com.example.keli.api.RetrofitManager;
import com.example.keli.api.http.api.BaseResponse;
import com.example.keli.api.http.api.subscriber.SubscriberListener;
import com.example.keli.base.BaseActivity;
import com.example.keli.base.utils.DensityUtil;
import com.example.keli.base.utils.ViewUtils;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * description: 助力榜列表
 * author: Allen
 * date: 2019/3/22 14:47
 */
public class TicketListActivity extends BaseActivity {

    String id = "";
    int flag = 0;//flag 0是助力榜，1是粉丝榜
    int page = 1;
    TicketAdapter adapter;
    List<TicketData.DataBean> mTicketList = new ArrayList<>();
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.refreshlayout)
    SwipeRefreshLayout refreshlayout;
    @BindView(R.id.jump_home)
    ImageView jumpHome;

    public static void start(Context context, String id, int flag) {
        Intent intent = new Intent(context, TicketListActivity.class);
        intent.putExtra("id", id);
        intent.putExtra("flag", flag);
        context.startActivity(intent);
    }

    @Override
    protected int layoutId() {
        return R.layout.activity_ticket_list;
    }

    @Override
    protected void bindData() {
        id = getIntent().getStringExtra("id");
        flag = getIntent().getIntExtra("flag", 0);
        recyclerview.setLayoutManager(new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL));
        recyclerview.setAdapter(adapter);
        recyclerview.addItemDecoration(
                new HorizontalDividerItemDecoration.Builder(TicketListActivity.this)
                        .color(ContextCompat.getColor(TicketListActivity.this, R.color.divider_line))
                        .size(DensityUtil.dip2px(TicketListActivity.this, 1))
                        .margin(DensityUtil.dip2px(12), DensityUtil.dip2px(12))
                        .build());
        adapter = new TicketAdapter(mTicketList, flag);
        recyclerview.setAdapter(adapter);
        if (flag == 0) {
            abTitle.setText("助力榜");
            getAnnouncementTicket();
        } else {
            abTitle.setText("粉丝榜");
            getAnnouncementGift();
        }
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowTitleEnabled(false);

        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void bindListener() {
        adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                page++;
                if (flag == 0) {
                    getAnnouncementTicket();
                } else {
                    getAnnouncementGift();
                }
            }
        }, recyclerview);
        refreshlayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                mTicketList.clear();
                if (flag == 0) {
                    getAnnouncementTicket();
                } else {
                    getAnnouncementGift();
                }
            }
        });
    }


    private void getAnnouncementTicket() {
        Map<String, String> map = new HashMap<>();
        map.put("id", id);
        map.put("page", String.valueOf(page));
        HttpUtils.ApiFunc(RetrofitManager.mApiFunc.getAnnouncementTicket(map), new SubscriberListener<BaseResponse<TicketData>>() {
            @Override
            public void onSuccess(BaseResponse<TicketData> ticketDataBaseResponse) {
                if (page == 1) {
                    refreshlayout.setRefreshing(false);
                    if(ticketDataBaseResponse.getData().getData().size() == 0){
                        adapter.setEmptyView(ViewUtils.getEmptyView(getActivity()));
                        return;
                    }
                }
                if (ticketDataBaseResponse.getData().getData().size() == 0) {
                    adapter.loadMoreEnd();
                    return;
                }
                mTicketList.addAll(ticketDataBaseResponse.getData().getData());
                adapter.notifyDataSetChanged();
                adapter.loadMoreComplete();
            }

            @Override
            public void onFailer(String msg) {

            }

            @Override
            public void onTokenError() {

            }
        });
    }

    private void getAnnouncementGift() {
        Map<String, String> map = new HashMap<>();
        map.put("id", id);
        map.put("page", String.valueOf(page));
        HttpUtils.ApiFunc(RetrofitManager.mApiFunc.getAnnouncementGift(map), new SubscriberListener<BaseResponse<TicketData>>() {
            @Override
            public void onSuccess(BaseResponse<TicketData> ticketDataBaseResponse) {
                if (page == 1) {
                    refreshlayout.setRefreshing(false);
                    if(ticketDataBaseResponse.getData().getData().size() == 0){
                        adapter.setEmptyView(ViewUtils.getEmptyView(getActivity()));
                        return;
                    }
                }
                if (ticketDataBaseResponse.getData().getData().size() == 0) {
                    adapter.loadMoreEnd();
                    return;
                }
                mTicketList.addAll(ticketDataBaseResponse.getData().getData());
                adapter.notifyDataSetChanged();
                adapter.loadMoreComplete();
            }

            @Override
            public void onFailer(String msg) {

            }

            @Override
            public void onTokenError() {

            }
        });
    }

    @OnClick(R.id.jump_home)
    public void onViewClicked() {
        MainActivity.start(TicketListActivity.this);
    }
}
